package com.htfyun.factorytest.factorytestui;

import android.Manifest;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.media.AudioManager;
import android.media.AudioSystem;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.SwitchCompat;
import androidx.collection.ArraySet;
import androidx.core.content.ContextCompat;

import com.blankj.utilcode.util.VolumeUtils;
import com.htfyun.factorytest.BluetoothBondManager.BluetoothBondManager;
import com.htfyun.factorytest.BluetoothBondManager.BluetoothDeviceFilter;
import com.htfyun.factorytest.BluetoothBondManager.OnBluetoothAllListener;
import com.htfyun.factorytest.BluetoothSco.BluetoothScoManager;
import com.htfyun.factorytest.BluetoothSco.OnBluetoothScoConnectedListener;
import com.htfyun.factorytest.R;
import com.htfyun.factorytest.common.Constants;
import com.htfyun.factorytest.common.FTUtils;
import com.htfyun.factorytest.common.PermissionManager;
import com.htfyun.factorytest.common.RecordAndPlayThread;
import com.htfyun.factorytest.config.EinkProperty;
import com.htfyun.factorytest.device.DeviceAttr;
import com.htfyun.factorytest.playerutils.MediaPlayerTool;
import com.htfyun.factorytest.utils.DebugLog;
import com.htfyun.factorytest.utils.executor.AppExecutors;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;


public class FactoryBluetoothActivity extends FactoryBaseActivity implements
        View.OnClickListener, AdapterView.OnItemClickListener,
        CompoundButton.OnCheckedChangeListener,
        OnBluetoothAllListener, OnBluetoothScoConnectedListener {

    private final Object flagScan = new Object();
    private final Object flagScanning = new Object();
    private Button btnScan;
    private ListView listFound;
    private ListView listBound;

    private TextView txtTips;
    private TextView txtInfo;

    private SwitchCompat btSwitch;

    private TextView txtBluetoothScoInfo;
    private Button btnTestBluetoothSco;
    private MediaPlayerTool mediaPlayerTool;
    private final Object flagScoStart = new Object();
    private final Object flagScoStop = new Object();

    private Button btnJumpToSelectBT;

    private Map<String, BluetoothDevice> foundBTDeviceMap = new HashMap<>();
    private Map<String, BluetoothDevice> boundBTDeviceMap = new HashMap<>();
    private ArraySet<BluetoothDevice> priority = new ArraySet<>();

    private BTDeviceAdapter foundDeviceAdapter;
    private BTDeviceAdapter boundDeviceAdapter;

    private BluetoothBondManager bluetoothBondManager;

    private BluetoothScoManager bluetoothScoManager;

    private AudioManager mAudioManager;

    private String[] TEST_BT_NAMES = {
            "S-35",
            "Philips BT110",
            "orange的AirPods",
            "A2",
            "K2",
//            "W-speaker",
//            "N-612",
//            "BT-speaker",
//            "HAVIT M8",
    };

    @Override
    int getLayoutId() {
        return R.layout.activity_factory_bluetooth;
    }

    @Override
    void onInitView(@Nullable Bundle savedInstanceState) {

        btnScan = findViewById(R.id.btnScan);
        txtTips = findViewById(R.id.txtTips);
        txtInfo = findViewById(R.id.txtInfo);
        listFound = findViewById(R.id.listFound);
        listBound = findViewById(R.id.listBound);

        btSwitch = findViewById(R.id.btSwitch);

        btSwitch.setThumbResource(R.drawable.switchbar_thumb);
        btSwitch.setTrackResource(R.drawable.switchbar_track);
        btSwitch.setOnCheckedChangeListener(this);


        txtBluetoothScoInfo = findViewById(R.id.txtBluetoothScoInfo);
        btnTestBluetoothSco = findViewById(R.id.btnTestBluetoothSco);
        btnJumpToSelectBT = findViewById(R.id.btnJumpToSelectBT);

        btnScan.setOnClickListener(this);
        btnTestBluetoothSco.setOnClickListener(this);
        btnJumpToSelectBT.setOnClickListener(this);

        mediaPlayerTool = MediaPlayerTool.getInstance(getContext(), getHandler());

        bluetoothBondManager = new BluetoothBondManager(mContext);
        bluetoothScoManager = new BluetoothScoManager(mContext);

        bluetoothBondManager.setEnabledListener(this);
        bluetoothBondManager.setBondListener(this);
        bluetoothBondManager.setDiscoveryListener(this);
        bluetoothBondManager.setFoundListener(this);
        bluetoothBondManager.setAclConnectedListener(this);

        bluetoothScoManager.setScoConnectedListener(this);

        if (!isBTGotPermission()) {
            initBTPermission();
        }

        if (!isRecordGotPermission()) {
            initRecordPermission();
        }

        foundDeviceAdapter = new BTDeviceAdapter(mContext, -1);
        boundDeviceAdapter = new BTDeviceAdapter(mContext, -1);

        listFound.setAdapter(foundDeviceAdapter);
        listBound.setAdapter(boundDeviceAdapter);

        listFound.setOnItemClickListener(this);
        listBound.setOnItemClickListener(this);

        setScoViewVisibility(false);
        setBtnPassVisibility(View.INVISIBLE);

        mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

        if (EinkProperty.isSuper()) {
            listBound.setVisibility(View.GONE);
            btnJumpToSelectBT.setVisibility(View.GONE);
            findViewById(R.id.layoutBound).setVisibility(View.GONE);
            txtTips.setText(getString(R.string.bt_test_desc_super));
        }
    }

    @Override
    public void onClick(View v) {

        if (v == btnScan) {

            if (btnScan.getTag() == flagScanning) {

                bluetoothBondManager.cancelDiscovery();

            } else {

                startDiscovery();
            }

        } else if (v == btnJumpToSelectBT) {

            if (DeviceAttr.getRunningDeviceAttr().hasBluetoothRecord()) {

                setScoViewVisibility(true);

            }

            startBTSettings();

        } else if (v == btnTestBluetoothSco) {

            if (btnTestBluetoothSco.getTag() == flagScoStop) {
                if (bluetoothBondManager != null) {
                    bluetoothScoManager.startRecorderScoMode();
                }

            } else {
                if (bluetoothBondManager != null) {
                    bluetoothScoManager.stopRecorderScoMode();
                    AppExecutors.postToMainThreadDelay(this::startPlay, 1000);
                }
            }

        }

    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

        setBluetoothEnabled(isChecked);
    }

    private void setSwitchStatus(boolean on) {
        btSwitch.setOnCheckedChangeListener(null);
        btSwitch.setChecked(on);
        btSwitch.setOnCheckedChangeListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();

        DebugLog.e("bt onResume");

        setSwitchStatus(bluetoothBondManager.isBluetoothEnabled());

        bluetoothBondManager.registerBTReceiver();
        startDiscovery();

        if (DeviceAttr.getRunningDeviceAttr().hasBluetoothRecord()) {
            bluetoothScoManager.registerBluetoothScoReceiver();
        } else {
            startTestPlaySound();
        }
    }

    @Override
    protected void onPause() {
        DebugLog.e("bt onPause");

        super.onPause();
        bluetoothBondManager.cancelDiscovery();
        bluetoothBondManager.unregisterBTReceiver();

        if (DeviceAttr.getRunningDeviceAttr().hasBluetoothRecord()) {
            bluetoothScoManager.unregisterBluetoothScoReceiver();
        } else {
            stopTestPlaySound();
        }
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        DebugLog.e("bt onWindowFocusChanged: hasFocus = " + hasFocus);
        if (hasFocus && btnTestBluetoothSco.getTag() == flagScoStart) {
            if (bluetoothBondManager != null) {
                bluetoothScoManager.stopRecorderScoMode();
                bluetoothScoManager.startRecorderScoMode();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        btOnDestroy();

        DebugLog.e("bt onDestroy: disable bt");
    }

    private MediaPlayer mediaPlayer;

    private void stopTestPlaySound() {

        DebugLog.e("stopTestPlaySound ");

        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.release();//回收资源
            mediaPlayer = null;
        }
    }

    private void startTestPlaySound() {
        if (mediaPlayer != null) {
            stopTestPlaySound();
        }
        try {
            mediaPlayer = MediaPlayer.create(getContext(), R.raw.music_test_channel);
            VolumeUtils.setVolume(AudioManager.STREAM_MUSIC, Constants.PCBA_VOLUME, 0);
            mediaPlayer.setLooping(true);//这onRecordingStop种这个后, setOnCompletionListener不能被调用了.
            mediaPlayer.start();
            DebugLog.e("startTestPlaySound ");

        } catch (Exception e) {
            DebugLog.e("exception e = " + e.getMessage());
        }

        mediaPlayer.setOnErrorListener((mp, what, extra) -> {
            DebugLog.e("onError, what = " + what + ", extra = " + extra);
            startTestPlaySound();
            return true;
        });

    }

    private void btOnDestroy() {
        DebugLog.e("btOnDestroy");
        stopRecordAndPlay();

        if (bluetoothScoManager != null) {

            bluetoothScoManager.setScoConnectedListener(null);
            bluetoothScoManager.stopRecorderScoMode();
            bluetoothScoManager = null;
        }


        if (bluetoothBondManager != null) {

            bluetoothBondManager.setEnabledListener(null);
            bluetoothBondManager.setBondListener(null);
            bluetoothBondManager.setDiscoveryListener(null);
            bluetoothBondManager.setFoundListener(null);
            bluetoothBondManager.setAclConnectedListener(null);

            bluetoothBondManager.cancelDiscovery();
            bluetoothBondManager.disconnectAllProfile();

            Set<BluetoothDevice> deviceList = bluetoothBondManager.getBluetoothAdapter().getBondedDevices();

            if (deviceList != null && !deviceList.isEmpty()) {
                for (BluetoothDevice device : deviceList) {
                    bluetoothBondManager.removeBond(device);
                }
            }

            bluetoothBondManager.closeBluetooth();
            bluetoothBondManager = null;
        }
    }


    private void startDiscovery() {

        if (bluetoothBondManager.isBluetoothEnabled()) {

            foundBTDeviceMap.clear();
            priority.clear();
            updateFondDeviceList();

            Set<BluetoothDevice> deviceList = bluetoothBondManager.getBluetoothAdapter().getBondedDevices();

            boundBTDeviceMap.clear();

            if (deviceList != null && !deviceList.isEmpty()) {
                for (BluetoothDevice device : deviceList) {
                    boundBTDeviceMap.put(device.getAddress(), device);
                }

            }
            updateBondDeviceList();

            bluetoothBondManager.startDiscovery();

            return;
        }

        setBluetoothEnabled(true);

    }

    private void setBluetoothEnabled(boolean enabled) {

        if (enabled) {
            bluetoothBondManager.openBluetooth();

        } else {
            bluetoothBondManager.cancelDiscovery();
            bluetoothBondManager.closeBluetooth();
        }

        boundBTDeviceMap.clear();
        foundBTDeviceMap.clear();
        updateBondDeviceList();
        updateFondDeviceList();

    }


    private void setScoViewVisibility(boolean visibility) {
        txtBluetoothScoInfo.setVisibility(visibility ? View.VISIBLE : View.INVISIBLE);
        btnTestBluetoothSco.setVisibility(visibility ? View.VISIBLE : View.INVISIBLE);
    }


    private void startBTSettings() {

        if (startAndroidBluetoothSettings()) {
            return;
        }

        if (startIflytekBluetoothSettings()) {
            return;
        }

        Toast.makeText(mContext, R.string.bt_no_settings, Toast.LENGTH_LONG).show();

    }

    private boolean startAndroidBluetoothSettings() {

        try {

            Intent intent = new Intent(Settings.ACTION_BLUETOOTH_SETTINGS);
            startActivity(intent);

        } catch (Exception e) {

            return false;
        }

        return true;
    }

    private boolean startIflytekBluetoothSettings() {

        try {

            Intent intent = new Intent(Intent.ACTION_MAIN);
            ComponentName cn = new ComponentName("com.iflytek.ainote.settings", "com.iflytek.ainote.settings.ui.activity.BlueToothActivity");
            intent.setComponent(cn);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);

        } catch (Exception e) {

            return false;
        }

        return true;

    }

    private void initRecordPermission() {
        //如果无权限就申请然后回调，如果有权限就执行录音监听
        if (!isRecordGotPermission()) {

            //同时申请多个权限
            PermissionManager.getInstance(getApplicationContext()).execute(this,
                    android.Manifest.permission.RECORD_AUDIO,
                    android.Manifest.permission.WRITE_SETTINGS,
                    android.Manifest.permission.WRITE_EXTERNAL_STORAGE);


        }

    }

    private boolean isRecordGotPermission() {

        return (ContextCompat.checkSelfPermission(mContext, android.Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED
                && ContextCompat.checkSelfPermission(mContext, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
                && ContextCompat.checkSelfPermission(mContext, android.Manifest.permission.WRITE_SETTINGS) == PackageManager.PERMISSION_GRANTED
        );

    }


    private boolean isBTGotPermission() {

        return (ContextCompat.checkSelfPermission(mContext, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
                && ContextCompat.checkSelfPermission(mContext, android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED
        );

    }

    private void initBTPermission() {

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                || ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

            //同时申请多个权限
            PermissionManager.getInstance(getApplicationContext()).execute(this,
                    Manifest.permission.ACCESS_FINE_LOCATION,
                    Manifest.permission.ACCESS_COARSE_LOCATION);

        }

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionManager.getInstance(getApplicationContext()).
                onRequestPermissionsResult(requestCode, permissions, grantResults);

    }

    /////////////////////////////////////////////////////////////////////////////////

    private void updateScoBtn() {
        runOnUiThread(() -> {

            if (btnTestBluetoothSco.getTag() == flagScoStart) {
                btnTestBluetoothSco.setText(R.string.bt_test_sco_stop);
            } else {
                btnTestBluetoothSco.setText(R.string.bt_test_sco_start);
            }

        });

    }

    /////////////////////////////////////////////////////////////////////////////////

    private void updateScanBtn() {
        runOnUiThread(() -> {

            if (bluetoothBondManager.isDiscovering()) {
                btnScan.setText(R.string.ft_bt_scanning);
                btnScan.setTag(flagScanning);
            } else {
                btnScan.setText(R.string.ft_bt_scan);
                btnScan.setTag(flagScan);
            }

        });

    }

    private synchronized void updateFondDeviceList() {
        foundDeviceAdapter.setNotifyOnChange(false);
        foundDeviceAdapter.clear();

        foundDeviceAdapter.setNotifyOnChange(true);

        for (String name : TEST_BT_NAMES) {
            Iterator<Map.Entry<String, BluetoothDevice>> it = foundBTDeviceMap.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry<String, BluetoothDevice> item = it.next();
                BluetoothDevice device = item.getValue();

                if (TextUtils.isEmpty(device.getName())) {
                    continue;
                }
                if (name.equals(device.getName())) {
                    priority.add(device);
                    it.remove();
                    DebugLog.e("remove device = " + device.getName());
                }
            }
        }
        foundDeviceAdapter.addAll(priority);
        foundDeviceAdapter.addAll(foundBTDeviceMap.values());
        DebugLog.e("priority size = " + priority.size());
        DebugLog.e("foundDeviceAdapter size = " + foundDeviceAdapter.getCount());
    }

    private synchronized void updateBondDeviceList() {
        boundDeviceAdapter.setNotifyOnChange(false);
        boundDeviceAdapter.clear();

        boundDeviceAdapter.setNotifyOnChange(true);
        boundDeviceAdapter.addAll(boundBTDeviceMap.values());
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        BluetoothDevice device = null;
        if (parent == listFound) {
            device = foundDeviceAdapter.getItem(position);
        } else if (parent == listBound) {
            device = boundDeviceAdapter.getItem(position);
        }

        if (device == null) {
            return;
        }
        DebugLog.e("device addr = " + device.getAddress() + ", name = " + device.getName());

        bluetoothBondManager.bondDevice(device);

    }

    @Override
    public void onBluetoothBonding(BluetoothDevice device) {

        DebugLog.e("onBluetoothBonding device = " + device);
        String name = device.getName();
        if (TextUtils.isEmpty(name)) {
            name = device.getAddress();
        }
        String status = getString(R.string.bt_bonding_status, name);
        updateTxtInfo(status);

        boundBTDeviceMap.put(device.getAddress(), device);
        foundBTDeviceMap.remove(device.getAddress());
        updateBondDeviceList();
        updateFondDeviceList();

    }

    @Override
    public void onBluetoothBonded(BluetoothDevice device) {
        DebugLog.e("onBluetoothBonded device = " + device);

        String name = device.getName();
        if (TextUtils.isEmpty(name)) {
            name = device.getAddress();
        }

        if (DeviceAttr.getRunningDeviceAttr().hasBluetoothRecord()) {
            String status = getString(R.string.bt_bonded_status, name);
            updateTxtInfo(status);
        } else {
            //设置蓝牙通道的music音量
            VolumeUtils.setVolume(AudioManager.STREAM_MUSIC, Constants.PCBA_VOLUME, 0);
            updateTxtInfo(getString(R.string.bt_normal_test_info));
        }

    }

    @Override
    public void onBluetoothConnected(BluetoothDevice device, int profile) {


        DebugLog.e("onBluetoothConnected profile = " + profile);

        if (profile == BluetoothProfile.HEADSET) {

            getHandler().sendEmptyMessageDelayed(MSG_START_RECORD_SCO_MODE, DELAY_START_RECORD_SCO_MODE);

        }
    }

    @Override
    public void onBluetoothDisconnected(BluetoothDevice device, int profile) {
        DebugLog.e("onBluetoothDisconnected profile = " + profile);
        updateBondDeviceList();

    }

    @Override
    public void onDiscoveryStarted() {
        updateScanBtn();
    }

    @Override
    public void onDiscoveryFinished() {
        updateScanBtn();
        if (EinkProperty.isSuper() && !foundBTDeviceMap.isEmpty()) {
            finishWithOK();
        }
    }

    @Override
    public void onBluetoothEnabled(boolean enabled) {
        setSwitchStatus(enabled);

        DebugLog.e("onBluetoothEnabled enable = " + enabled);
        if (enabled) {

            startDiscovery();

        } else {
            if (bluetoothBondManager != null) {
                bluetoothScoManager.stopRecorderScoMode();
                stopRecordAndPlay();
                setScoViewVisibility(false);
                String status = getString(R.string.bt_status_off);
                updateTxtInfo(status);
            }
        }
    }

    @Override
    public void onBluetoothFound(BluetoothDevice device) {

        if (foundBTDeviceMap.get(device.getAddress()) == null) {

            if (!BluetoothDeviceFilter.AUDIO_FILTER.matches(device)) {
                DebugLog.e(device.getName() + " is not audio device, not support");
                return;
            }
            foundBTDeviceMap.put(device.getAddress(), device);
            updateFondDeviceList();
        }

    }

    @Override
    public void onBluetoothScoConnected(boolean connected) {
        if (!DeviceAttr.getRunningDeviceAttr().hasBluetoothRecord()) {
            return;
        }

        DebugLog.e("onBluetoothScoConnected connected = " + connected);

        if (connected) {
            startRecordAndPlay();
        } else {
            stopRecordAndPlay();
        }

    }

    @Override
    public void onBluetoothScoFailed() {

        runOnUiThread(() -> {
            Toast.makeText(mContext, R.string.bt_test_sco_connected_failed, Toast.LENGTH_LONG).show();

            finishWithFail();
        });

    }

    @Override
    public void onBluetoothACLConnected(BluetoothDevice device, boolean connected) {

        DebugLog.e("onBluetoothACLConnected = " + connected);

        if (!connected) {

            onBluetoothDisconnected(device);

        } else {

            if (boundBTDeviceMap.get(device.getAddress()) != null) {
                getHandler().sendEmptyMessageDelayed(MSG_START_RECORD_SCO_MODE, DELAY_START_RECORD_SCO_MODE);
            }
        }
    }

    private void onBluetoothDisconnected(BluetoothDevice device) {

        setBtnPassVisibility(View.INVISIBLE);
        setScoViewVisibility(false);
        if (bluetoothBondManager != null) {
            bluetoothScoManager.stopRecorderScoMode();
            bluetoothBondManager.removeBond(device);
        }

        String name = device.getName();
        if (TextUtils.isEmpty(name)) {
            name = device.getAddress();
        }
        String status = getString(R.string.bt_disconnected_status, name);
        updateTxtInfo(status);

        startDiscovery();
    }

    private class BTDeviceAdapter extends ArrayAdapter<BluetoothDevice> {

        public BTDeviceAdapter(@NonNull Context context, int resource) {
            super(context, resource);
        }

        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
//            ViewHolder holder = null;
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = getLayoutInflater().inflate(R.layout.item_ft_bt_devices, null);
                holder.txtName = convertView.findViewById(R.id.txtName);
//                holder.txtRssi = convertView.findViewById(R.id.txtRssi);

                convertView.setTag(holder);
            } else {
//            if (holder == null) {
                holder = (ViewHolder) convertView.getTag();
            }
            BluetoothDevice device = getItem(position);

            if (device != null) {

                String name = device.getName();
                if (TextUtils.isEmpty(name)) {
                    name = device.getAddress();
                }
                holder.txtName.setText(name);
                DebugLog.e("name = " + name);
            }

            return convertView;
        }
    }

    private static final class ViewHolder {
        private TextView txtName;
//        private TextView txtRssi;
    }

    private void updateTxtInfo(String txt) {
        getHandler().obtainMessage(MSG_UPDATE_TXT_INFO, txt).sendToTarget();

    }

    ////////////////////////////////////////////////
    ////////////////////////////////////////////////

    private final static int MSG_UPDATE_TXT_INFO = 2000;

    private final static int MSG_SET_BTNPASS_VISIBLE = 2001;
    private final static int DELAY_SET_BTNPASS_VISIBLE = 8000;//5s

    private final static int MSG_START_RECORD_SCO_MODE = 2002;
    private final static int DELAY_START_RECORD_SCO_MODE = 2500;//2s


    @Override
    void onHandleMessage(Handler handler, Message msg) {

        switch (msg.what) {
            case MSG_UPDATE_TXT_INFO:
                if (msg.obj instanceof String) {
                    txtInfo.setText((String) msg.obj);
                }
                break;
            case MSG_SET_BTNPASS_VISIBLE:
                setBtnPassVisibility(View.VISIBLE);
                break;
            case MSG_START_RECORD_SCO_MODE:

                int delay = DELAY_SET_BTNPASS_VISIBLE / 4;

                if (DeviceAttr.getRunningDeviceAttr().hasBluetoothRecord() && bluetoothBondManager != null) {
                    setScoViewVisibility(true);
                    bluetoothScoManager.startRecorderScoMode();
                    delay = DELAY_SET_BTNPASS_VISIBLE;
                }

                handler.sendEmptyMessageDelayed(MSG_SET_BTNPASS_VISIBLE, delay);


                break;
        }

    }

    private void startPlay() {
        VolumeUtils.setVolume(AudioManager.STREAM_MUSIC, VolumeUtils.getMaxVolume(AudioSystem.STREAM_BLUETOOTH_SCO), 0);
//        mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, mAudioManager.getStreamMaxVolume(AudioSystem.STREAM_BLUETOOTH_SCO), 1);
        stopPlay();
        mediaPlayerTool.playPcm(RECORDER_FILE_PATH);

    }

    private void stopPlay() {
        mediaPlayerTool.stop();

    }

    private RecordAndPlayThread recordAndPlayThread = null;
    private static final String RECORDER_FILE_PATH = FTUtils.getFTStorageDirectory() + File.separator + "sco_test.pcm";

    private void startRecordAndPlay() {

        DebugLog.e("startRecordAndPlay------------");

        btnTestBluetoothSco.setTag(flagScoStart);
        updateScoBtn();

        if (recordAndPlayThread == null) {
            recordAndPlayThread = new RecordAndPlayThread("sco_test", mContext, /*AudioManager.STREAM_BLUETOOTH_SCO*/6, false);
            recordAndPlayThread.start();
        }

    }

    private void stopRecordAndPlay() {

        DebugLog.e("stopRecordAndPlay----------------");

        btnTestBluetoothSco.setTag(flagScoStop);
        updateScoBtn();

        if (recordAndPlayThread != null) {
            recordAndPlayThread.joinThread();
            recordAndPlayThread = null;
        }

    }

}
